﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PET.Tools;

namespace PET.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class VipController : ControllerBase
    {
        private readonly PetDbContext db;
        public VipController(PetDbContext _context)
        {
            db = _context;

        }

        //查询所有用户护理数据
        [HttpGet]
        public Result Get()
        {
            Result result = new Result();
            result.Data = db.Pet_Vip.ToList();
            return result;
        }

        //根据id查询信息
        [HttpGet("{id}")]
        public Result GetId(Guid id)
        {
            Result result = new Result();
            result.Data = db.Pet_Vip.Where(w => w.Id == id).SingleOrDefault();
            return result;
        }

        //用户数据添加
        [HttpPost]
        public Result AddPet([FromBody] Pet_Vip care)
        {
            Result result = new Result();
            result.Data = db.Pet_Vip.Add(care);
            result.Data = db.SaveChanges();
            return result;
        }

        //用户数据更改
        [HttpPut]
        public Result Put([FromBody] Pet_Vip care)
        {
            Result result = new Result();
            result.Data = db.Pet_Vip.Update(care);
            result.Data = db.SaveChanges();
            return result;
        }

        //软删除
        [HttpDelete("{id}")]
        public Result Del(Guid id)
        {
            Result result = new Result();
            Pet_Vip? vip = new Pet_Vip();
            vip = db.Pet_Vip.Where(s => s.Id == id).FirstOrDefault();
            if (vip != null)
            {
                vip.DelTime = DateTime.Now;
                db.Pet_Vip.Update(vip);
                result.Data = db.SaveChanges();
            }
            return result;
        }

        //根据VIP等级进行排序
        [HttpGet]
        public List<Pet_Vip> Orderby()
        {
            return db.Pet_Vip.OrderBy(s => s.Membershiplevel).ToList();
        }

        #region 根据会员号，联系电话，姓名查询多个字段

        [HttpGet]
        public IQueryable<Pet_Vip> GetBySelect([FromHeader] string memberId, [FromHeader] string name, [FromHeader] string phone)
        {
            IQueryable<Pet_Vip> query= db.Pet_Vip;
            if (!string.IsNullOrEmpty(memberId))
            {
                query = query.Where(s => s.MemberId == memberId);
            }
            if (!string.IsNullOrEmpty(name))
            {
                query = query.Where(s => s.Name == name);
            }
            if (!string.IsNullOrEmpty(phone))
            {
                query = query.Where(s => s.Phone == phone);
            }
            return query;
        }

        #endregion
    }
}
